From f788ea84e25a4f7216ca0604aeb216346403b6ef Mon Sep 17 00:00:00 2001
From: Owen Jacobson <owen@grimoire.ca>
Date: Tue, 25 Feb 2025 00:37:33 -0500
Subject: Track state on a per-session basis, rather than via globals.

Sorry about the thousand-line omnibus change; this is functionally a rewrite of the client's state tracking, flavoured to resemble the existing code as far as is possible, rather than something that can be parted out and committed in pieces.

Highlights:

* No more `store.writeable()`s. All state is now tracked using state runs or derivatives. State is still largely structured the way it was, but several bits of nested state have been rewritten to ensure that their properties are reactive just as much as their containers are.
* State is no longer global. `(app)/+layout` manages a stateful session, created via its load hook and started/stopped via component mount and destroy events. The session also tracks an event source for the current state, and feeds events into the state, broadly along the same lines as the previous stores-based approach.

Together these two changes fix up several rough spots integrating state with Svelte, and allow for the possibility of multiple states. This is a major step towards restartable states, and thus towards better connection management, which will require the ability to "start over" once a connection is restored.
---
 ui/routes/(app)/ch/[channel]/+page.svelte | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)

(limited to 'ui/routes/(app)/ch/[channel]/+page.svelte')

diff --git a/ui/routes/(app)/ch/[channel]/+page.svelte b/ui/routes/(app)/ch/[channel]/+page.svelte
index 095e66a..c8507cc 100644
--- a/ui/routes/(app)/ch/[channel]/+page.svelte
+++ b/ui/routes/(app)/ch/[channel]/+page.svelte
@@ -3,24 +3,17 @@
   import { page } from '$app/state';
   import ActiveChannel from '$lib/components/ActiveChannel.svelte';
   import MessageInput from '$lib/components/MessageInput.svelte';
-  import { channelsList, channelsMetaList, currentUser, logins, messages } from '$lib/store';
+  import { runs } from '$lib/runs.js';
   import * as api from '$lib/apiServer';
 
-  let channel = $derived(page.params.channel);
-  let messageRuns = $derived(
-    $messages.inChannel(channel).map(({ sender, messages }) => {
-      let senderName = $derived($logins.get(sender));
-      let ownMessage = $derived($currentUser !== null && $currentUser.id === sender);
-
-      return {
-        sender: senderName,
-        ownMessage,
-        messages
-      };
-    })
-  );
+  const { data } = $props();
+  const { session } = data;
   let activeChannel;
 
+  const channel = $derived(page.params.channel);
+  const messages = $derived(session.messages.filter((message) => message.channel === channel));
+  const messageRuns = $derived(runs(messages, session.currentUser));
+
   function inView(parentElement, element) {
     const parRect = parentElement.getBoundingClientRect();
     const parentTop = parRect.top;
@@ -49,12 +42,12 @@
     const lastInView = getLastVisibleMessage();
     if (lastInView) {
       const at = DateTime.fromISO(lastInView.dataset.at);
-      $channelsMetaList.updateLastReadAt(channel, at);
+      session.local.updateLastReadAt(channel, at);
     }
   }
 
   $effect(() => {
-    const _ = $messages.inChannel(channel);
+    const _ = session.messages;
     setLastRead();
   });
 
-- 
cgit v1.2.3

